// Copyright (c) Microsoft Corporation.  All rights reserved.

#include "stdafx.h"
#include "D3D10Effect.h"

#include "D3D10EffectConstantBuffer.h"
#include "D3D10Device.h"
#include "D3D10Device1.h"
#include "D3D10EffectTechnique.h"
#include "D3D10EffectVariable.h"

using namespace System::Runtime::InteropServices;
using namespace msclr::interop;
using namespace Microsoft::WindowsAPICodePack::DirectX::Direct3D10;

EffectConstantBuffer^ Effect::GetConstantBufferByIndex(UInt32 index)
{
    ID3D10EffectConstantBuffer* returnValue = GetInterface<ID3D10Effect>()->GetConstantBufferByIndex(static_cast<UINT>(index));
    return gcnew EffectConstantBuffer(returnValue);
}

EffectConstantBuffer^ Effect::GetConstantBufferByName(String^ name)
{
    marshal_context^ context = gcnew marshal_context();
    ID3D10EffectConstantBuffer* returnValue = GetInterface<ID3D10Effect>()->GetConstantBufferByName(context->marshal_as<const char*>(name));
    return returnValue == NULL ? nullptr : gcnew EffectConstantBuffer(returnValue);
}

EffectDescription^ Effect::GetDescription()
{
    D3D10_EFFECT_DESC* tempoutDescription = new D3D10_EFFECT_DESC();
    CommonUtils::VerifyResult(GetInterface<ID3D10Effect>()->GetDesc(tempoutDescription));
    
    return gcnew EffectDescription(tempoutDescription);
}

D3DDevice^ Effect::GetDevice()
{
    ID3D10Device* tempoutDevice = NULL;
    CommonUtils::VerifyResult(GetInterface<ID3D10Effect>()->GetDevice(&tempoutDevice));
    
    return tempoutDevice == NULL ? nullptr : gcnew D3DDevice(tempoutDevice);
}

D3DDevice1^ Effect::GetDevice1()
{
    ID3D10Device* device = NULL;
    CommonUtils::VerifyResult(GetInterface<ID3D10Effect>()->GetDevice(&device));
    
    ID3D10Device1* tempoutDevice = NULL;
    CommonUtils::VerifyResult(device->QueryInterface(__uuidof(ID3D10Device1),(void**) tempoutDevice));
    return tempoutDevice == NULL ? nullptr : gcnew D3DDevice1(tempoutDevice);
}

EffectTechnique^ Effect::GetTechniqueByIndex(UInt32 index)
{
    ID3D10EffectTechnique* returnValue = GetInterface<ID3D10Effect>()->GetTechniqueByIndex(static_cast<UINT>(index));
    return gcnew EffectTechnique(returnValue);
}

EffectTechnique^ Effect::GetTechniqueByName(String^ name)
{
    marshal_context^ context = gcnew marshal_context();
    ID3D10EffectTechnique* returnValue = GetInterface<ID3D10Effect>()->GetTechniqueByName(context->marshal_as<const char*>(name));
    return returnValue == NULL ? nullptr : gcnew EffectTechnique(returnValue, false);
}

EffectVariable^ Effect::GetVariableByIndex(UInt32 index)
{
    ID3D10EffectVariable* returnValue = GetInterface<ID3D10Effect>()->GetVariableByIndex(static_cast<UINT>(index));
    return gcnew EffectVariable(returnValue);
}

EffectVariable^ Effect::GetVariableByName(String^ name)
{
    marshal_context^ context = gcnew marshal_context();
    ID3D10EffectVariable* returnValue = GetInterface<ID3D10Effect>()->GetVariableByName(context->marshal_as<const char*>(name));
    return returnValue == NULL ? nullptr : gcnew EffectVariable(returnValue, false);
}

EffectVariable^ Effect::GetVariableBySemantic(String^ semantic)
{
    marshal_context^ context = gcnew marshal_context();
    ID3D10EffectVariable* returnValue = GetInterface<ID3D10Effect>()->GetVariableBySemantic(context->marshal_as<const char*>(semantic));
    return returnValue == NULL ? nullptr : gcnew EffectVariable(returnValue);
}

Boolean Effect::IsOptimized::get()
{
    return GetInterface<ID3D10Effect>()->IsOptimized() != 0;
}

Boolean Effect::IsPool::get()
{
    return GetInterface<ID3D10Effect>()->IsPool() != 0;
}

Boolean Effect::IsValid::get()
{
    return GetInterface<ID3D10Effect>()->IsValid() != 0;
}

void Effect::Optimize()
{
    CommonUtils::VerifyResult(GetInterface<ID3D10Effect>()->Optimize());
}

